package com.rock.code.datacollconfig.coll.service.impl;


import com.rock.code.datacollconfig.coll.condition.DataCollCondition;
import com.rock.code.datacollconfig.coll.dao.IDataCollDao;
import com.rock.code.datacollconfig.coll.dao.IDataCollModeDao;
import com.rock.code.datacollconfig.coll.domain.DataColl;
import com.rock.code.datacollconfig.coll.domain.DataCollMode;
import com.rock.code.datacollconfig.coll.service.IDataCollService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 采集配置service
 * @author wangsla
 *	2013.1.6
 */
@Component
public class DataCollServiceImpl implements IDataCollService {
	@Resource
	private IDataCollDao dataCollDao;
	@Resource
	private IDataCollModeDao dataCollModeDao;

	public void deleteDataCollByIds(String[] ids) throws Exception {
		// 删除采集之前，要删除对应的采集方式配置
		for (String id : ids) {
			// 根据采集id查询采集方式
			DataCollMode dataCollMode = dataCollModeDao.getDataCollModeByDataCollId(id);
			// 采集方式
			String collMode = dataCollMode.getId();
			
			if (!DataCollMode.DATA_COLL_MODE_IF.equals(collMode)) { // 如果不是接口采集
				Map<String, String> info = new HashMap<String, String>();
				info.put("dataCollId", id);
				info.put("tableName", dataCollMode.getTableName());
				info.put("column", dataCollMode.getFkey());
				
				dataCollDao.deleteDataCollModeConfigByDataCollId(info); // 删除采集方式配置信息
			}
		}
		dataCollDao.deleteDataCollByIds(ids); // 最后删除对应采集信息
	}


	public DataColl getDataCollById(String id) {
		return dataCollDao.getDataCollById(id);
	}




	public List<DataColl> listDataCollByCondition(DataCollCondition condition) {
		return dataCollDao.listDataCollByCondition(condition);
	}


	public void updateDataColl(DataColl dataColl) {
		dataCollDao.updateDataColl(dataColl); //更新
	}

	public IDataCollDao getDataCollDao() {
		return dataCollDao;
	}

	public void setDataCollDao(IDataCollDao dataCollDao) {
		this.dataCollDao = dataCollDao;
	}

	public IDataCollModeDao getDataCollModeDao() {
		return dataCollModeDao;
	}

	public void setDataCollModeDao(IDataCollModeDao dataCollModeDao) {
		this.dataCollModeDao = dataCollModeDao;
	}

}
